package com.jic.member.impl;

import com.jic.common.base.orm.Pagination;
import com.jic.member.constant.enums.NodeTemplateTypeEnum;
import com.jic.member.constant.enums.TripTimingJobTypeEnum;
import com.jic.member.mapper.TripBranchNodeEntityMapper;
import com.jic.member.mapper.TripExecuteInfoEntityMapper;
import com.jic.member.mapper.TripPeopleEntityMapper;
import com.jic.member.entity.TripBranchNodeEntity;
import com.jic.member.entity.TripExecuteInfoEntity;
import com.jic.member.entity.TripPeopleEntity;
import com.jic.member.service.TimedTriggerNodeService;
import com.jic.member.service.TripTimingJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yinhaijun
 * @date: 2020/6/8
 */
@Slf4j
@Service("timedTriggerNodeService")
public class TimedTriggerNodeServiceImpl implements TimedTriggerNodeService {

    @Autowired
    private TripBranchNodeEntityMapper tripBranchNodeEntityMapper;
    @Autowired
    private TripPeopleEntityMapper tripPeopleEntityMapper;
    @Autowired
    private TripTimingJobService tripTimingJobService;
    @Autowired
    private TripExecuteInfoEntityMapper tripExecuteInfoEntityMapper;
    @Override
    public void exe(TripBranchNodeEntity node) {
        log.info("进入定时触发服务");
        // 查询出下一个节点
        TripBranchNodeEntity tripBranchNodeEntityQ = new TripBranchNodeEntity();
        tripBranchNodeEntityQ.setPrevBranchNodeId(node.getId());
        tripBranchNodeEntityQ.setDeleteFlag(0);
        TripBranchNodeEntity tripBranchNodeEntityN = tripBranchNodeEntityMapper.query(tripBranchNodeEntityQ);
        // 查询出该旅程分支下的所有用户，考虑到数据量，我们一次性查100条，并分批放入job中
        List<TripPeopleEntity> tripPeopleEntityList;
        int pageNo = 1;
        do {
            TripPeopleEntity tripPeopleEntityQ = new TripPeopleEntity();
            tripPeopleEntityQ.setBranchId(node.getBranchId());
            tripPeopleEntityQ.setDeleteFlag(0);
            tripPeopleEntityList = tripPeopleEntityMapper.findOfPage(tripPeopleEntityQ, Pagination.build(pageNo, 100));
            // 注册Job
            if((null != tripPeopleEntityList && !tripPeopleEntityList.isEmpty())){
                List<String> thirdUserIdList = tripPeopleEntityList.stream().map(TripPeopleEntity::getThirdUserId).collect(Collectors.toList());
                tripTimingJobService.register(TripTimingJobTypeEnum.EXE_ACTION, tripBranchNodeEntityN.getNodeTemplateCode(), tripBranchNodeEntityN.getId(), null,
                        NodeTemplateTypeEnum.getByCode(tripBranchNodeEntityN.getNodeTemplateCode()).getMessage(), tripBranchNodeEntityN.getTripId(), tripBranchNodeEntityN.getId(), thirdUserIdList);
                thirdUserIdList.forEach(thirdUserId ->{
                    TripExecuteInfoEntity tripExecuteInfoEntity = new TripExecuteInfoEntity();
                    tripExecuteInfoEntity.setThirdUserId(thirdUserId);
                    tripExecuteInfoEntity.setBranchId(node.getBranchId());
                    tripExecuteInfoEntity.setTripId(node.getTripId());
                    tripExecuteInfoEntity.setBranchNodeId(node.getId());
                    tripExecuteInfoEntity.setNodeTemplateCode(node.getNodeTemplateCode());
                    tripExecuteInfoEntity.setRequestVariable(null);
                    tripExecuteInfoEntity.setStatus(1);
                    tripExecuteInfoEntity.setType(2);
                    tripExecuteInfoEntityMapper.insert(tripExecuteInfoEntity);
                });
            }
            pageNo++;
        }while (null != tripPeopleEntityList && !tripPeopleEntityList.isEmpty());

    }

}
